Skip to content

Refactor sudo handling for package installation#83

Open
tannevaled wants to merge 3 commits intopkgxdev:mainfrom
tannevaled:dev
Open

Refactor sudo handling for package installation#83
tannevaled wants to merge 3 commits intopkgxdev:mainfrom
tannevaled:dev

Conversation

@tannevaled
Copy link
Copy Markdown

proposal to fix sudo handling

Copilot AI review requested due to automatic review settings May 4, 2026 12:15
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR proposes to refactor how pkgm decides when to invoke pkgx directly vs wrapping it with sudo, specifically around installs targeting /usr/local.

Changes:

  • Introduces explicit isRoot and SUDO_USER detection in query_pkgx.
  • Emits a warning when installing to /usr/local as root without being invoked via sudo.
  • Changes the conditions under which /usr/bin/sudo is used and how the sudo arguments are constructed.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkgm.ts Outdated
Comment on lines +311 to +319
const needs_sudo_backwards =
install_prefix().string == "/usr/local" && !isRoot && !sudoUser;

let cmd = pkgx;

if (needs_sudo_backwards) {
cmd = "/usr/bin/sudo";
const user = Deno.env.get("USER")!;
args.unshift("-u", user, pkgx);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this one seems like the problem; we need drop privileges so we don't mess up a user's permissions, i believe.

Comment thread pkgm.ts
Comment on lines 315 to 323

if (needs_sudo_backwards) {
cmd = "/usr/bin/sudo";
const user = Deno.env.get("USER")!;
args.unshift("-u", user, pkgx);
}

const proc = new Deno.Command(cmd, {
args: [...args, "--json=v1"],
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems like a lateral fix that was either always needed, or isn't now needed.

Comment thread pkgm.ts

if (needs_sudo_backwards) {
cmd = "/usr/bin/sudo";
const user = Deno.env.get("USER")!;
Comment thread pkgm.ts
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 4, 2026 16:44
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkgm.ts Outdated
Comment on lines +312 to +322
const needs_sudo_backwards =
prefix == "/usr/local" && !isRoot && !sudoUser;

let cmd = pkgx;

if (needs_sudo_backwards) {
cmd = "/usr/bin/sudo";
const user = Deno.env.get("USER")!;
args.unshift("-u", user, pkgx);
}

Comment thread pkgm.ts
Comment on lines +319 to +320
const user = Deno.env.get("USER")!;
args.unshift("-u", user, pkgx);
Comment thread pkgm.ts
Comment on lines +316 to 324

if (needs_sudo_backwards) {
cmd = "/usr/bin/sudo";
const user = Deno.env.get("USER")!;
args.unshift("-u", user, pkgx);
}

const proc = new Deno.Command(cmd, {
args: [...args, "--json=v1"],
Comment thread pkgm.ts

if (needs_sudo_backwards) {
cmd = "/usr/bin/sudo";
const user = Deno.env.get("USER")!;
Comment thread pkgm.ts Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings May 5, 2026 06:48
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkgm.ts
Comment on lines +312 to +320
const needs_sudo_backwards =
prefix == "/usr/local" && !isRoot && !sudoUser;

let cmd = pkgx;

if (needs_sudo_backwards) {
if (!Deno.env.get("SUDO_USER")) {
if (Deno.uid() == 0) {
console.error(
"%cwarning",
"color:yellow",
"installing as root; installing via `sudo` is preferred",
);
}
cmd = pkgx;
} else {
args.unshift("-u", Deno.env.get("SUDO_USER")!, pkgx);
}
cmd = "/usr/bin/sudo";
const user = Deno.env.get("USER")!;
args.unshift("-u", user, pkgx);
Comment thread pkgm.ts
Comment on lines +318 to 321
cmd = "/usr/bin/sudo";
const user = Deno.env.get("USER")!;
args.unshift("-u", user, pkgx);
}
Comment thread pkgm.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants